﻿using System;
using System.Windows.Forms;

namespace RegexSharp
{
    public partial class Form_CustomSnippets : Form
    {
        private TreeNode _rootNode = new TreeNode("Custom Snippets");

        private bool _beChanged = false;

        public Form_CustomSnippets()
        {
            InitializeComponent();
        }

        private TreeNode CreateNodeStructure(Core.Snippet snippet)
        {
            TreeNode node = new TreeNode();
            node.Text = snippet.Name;
            node.Tag = snippet.Value;

            for (int i = 0; i < snippet.Subs.Count; i++)
            {
                node.Nodes.Add(CreateNodeStructure(snippet.Subs[i]));
            }

            return node;
        }

        private Core.Snippet SaveNodeStructure(TreeNode node)
        {
            Core.Snippet snippet = new Core.Snippet();
            snippet.Name = node.Text;
            snippet.Value = node.Tag as string;

            for (int i = 0; i < node.Nodes.Count; i++)
            {
                snippet.Subs.Add(SaveNodeStructure(node.Nodes[i]));
            }

            return snippet;
        }

        private void SaveCustomSnippets()
        {
            Config.Global.CustomSnippets = SaveNodeStructure(_rootNode);

            Helper.Serializer<Core.Snippet> snippetSerializer = new Helper.Serializer<Core.Snippet>();
            snippetSerializer.Serialize(Config.Global.CustomSnippets, Config.Global.CustomSnippetsFile);
        }

        private bool SelectedNodeIsNotRoot()
        {
            return treeView_CustomSnippets.SelectedNode != _rootNode;
        }

        private void Form_CustomSnippets_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (_beChanged)
            {
                DialogResult dr = MessageBox.Show("Do you want to save changes?", "Save", MessageBoxButtons.YesNoCancel);

                switch (dr)
                {
                    case DialogResult.Cancel:
                        e.Cancel = true;
                        break;
                    case DialogResult.No:
                        break;
                    case DialogResult.Yes:
                        SaveCustomSnippets();
                        break;
                    default:
                        break;
                }
            }
        }

        private void Form_CustomSnippets_Load(object sender, EventArgs e)
        {
            if (Config.Global.CustomSnippets != null)
            {
                _rootNode = CreateNodeStructure(Config.Global.CustomSnippets);
            }

            treeView_CustomSnippets.Nodes.Add(_rootNode);
            treeView_CustomSnippets.ExpandAll();
        }

        private void treeView_CustomSnippets_AfterSelect(object sender, TreeViewEventArgs e)
        {
            if (SelectedNodeIsNotRoot())
            {
                toolStripTextBox_Name.Text = treeView_CustomSnippets.SelectedNode.Text;
                toolStripTextBox_Value.Text = treeView_CustomSnippets.SelectedNode.Tag as string;
            }
        }

        private void toolStripButton_Add_Click(object sender, EventArgs e)
        {
            treeView_CustomSnippets.SelectedNode.Nodes.Add(new TreeNode("New - Click here and edit"));
            treeView_CustomSnippets.ExpandAll();

            _beChanged = true;
        }

        private void toolStripTextBox_Name_TextChanged(object sender, EventArgs e)
        {
            if (SelectedNodeIsNotRoot())
            {
                treeView_CustomSnippets.SelectedNode.Text = toolStripTextBox_Name.Text;

                _beChanged = true;
            }
        }

        private void toolStripTextBox_Value_TextChanged(object sender, EventArgs e)
        {
            if (SelectedNodeIsNotRoot())
            {
                treeView_CustomSnippets.SelectedNode.Tag = toolStripTextBox_Value.Text;

                _beChanged = true;
            }
        }

        private void toolStripButton_Delete_Click(object sender, EventArgs e)
        {
            if (SelectedNodeIsNotRoot())
            {
                treeView_CustomSnippets.SelectedNode.Remove();

                _beChanged = true;
            }
        }

        private void toolStripMenuItem_File_Save_Click(object sender, EventArgs e)
        {
            SaveCustomSnippets();

            _beChanged = false;
        }

        private void toolStripMenuItem_File_Exit_Click(object sender, EventArgs e)
        {
            Close();
        }
    }
}
